'''
Created on Oct 8, 2011

@author: oabalbin
'''

import logging
import os
import glob
import sys
import subprocess
from optparse import OptionParser
from config import ExomePipelineConfig, ExomeAnalysisConfig
from base import JOB_SUCCESS, JOB_ERROR


def list_analysis_files(dir,ext):
    '''
    '''
    files = glob.glob(os.path.join(dir,'*.'+ext))
    return files

def check_raw_seq_exists(analysis_file,output_dir=None):
    '''
    '''
    #config = ExomePipelineConfig()
    #config.from_xml(config_file)
    analysis = ExomeAnalysisConfig()
    analysis.from_xml(analysis_file, output_dir)
    sp_ready=[]
    sp_not_ready=[]
    
    for sample in analysis.samples:
        for lane in sample.lanes:
            for mate in xrange(len(lane.src_fastq_files)):
                src_fastq_file = lane.src_fastq_files[mate]
                # Check that all need sequence files are present in repository 
                if os.path.exists(src_fastq_file) or os.path.exists(src_fastq_file+'.gz'):
                    sp_ready.append(sample.name+'@@'+src_fastq_file)
                else:
                    sp_not_ready.append(sample.name+'@@'+src_fastq_file)
                    #logging.info("Local fastq file %s does not exists" % src_fastq_file)
                    
    if not sp_not_ready:
        return JOB_SUCCESS
    else:
        for sp in sp_not_ready:
            name,sfq=sp.split('@@')
            logging.info("Local fastq file %s for sample %s was not found" % (sfq,name))
        return JOB_ERROR
        
    
def main():
    logging.basicConfig(level=logging.DEBUG,
                        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    
    optionparser = OptionParser("usage: %prog [options] ")
    
    optionparser.add_option("-r", "--config_file", dest="config_file",
                            help="file with run configuration")
    optionparser.add_option("-a", "--analysis_file", dest="analysis_file",
                            help="file with experiment configuration") 
    
    
    (options, args) = optionparser.parse_args()
    
    
    config = ExomePipelineConfig()
    config.from_xml(options.config_file)
    
    # Hard Code for the shell script.
    shell_script=[]
    
    header='#!/bin/bash'
    cac_server='oabalbin@flux-login.engin.umich.edu'
    call_python = '/nobackup/med-mctp/sw/dev/python/epd/epd-7.0-2-rh5-x86_64/bin/python'
    call_pipe = '/home2/oabalbin/workspace/debug/exome/trunk/exome/jobs/run_analysis_bypatient.py'
    cac_bash_profile= '/home2/oabalbin/.bash_profile'
    irun_at = '--cluster'
    shell_script.append(header)
    ######
    analysisToRun_dir='/exds/users/oabalbin/projects/exomes/analysisToRun/'
    ext='xml'
        
    analysisToRun = list_analysis_files(analysisToRun_dir,ext)
    
    if not analysisToRun:
        logging.info("There were not analysis to run at this particular time")
        return
    
    for analysis_file in analysisToRun:
        if not ( check_raw_seq_exists(analysis_file, analysisToRun_dir) ):
            
            # Create analysis object
            analysis = ExomeAnalysisConfig()
            analysis.from_xml(analysis_file,analysisToRun_dir)
            ## files
            config_file_name=options.config_file.split('/')[-1]
            analysis_file_name=analysis_file.split('/')[-1]
            
            log_file_name=analysis.name+'.log'
            shell_file_name=analysis_file+'.sh'
            shell_file=open(os.path.join(analysisToRun_dir,shell_file_name),'w')
            
            cac_mkdir=['ssh',cac_server,'\'mkdir', analysis.remote_root_dir,
                        os.path.join(analysis.remote_root_dir,'lane'),
                        os.path.join(analysis.remote_root_dir,'sample'),
                        os.path.join(analysis.remote_root_dir,'analysis')+'\'']
            
            # Make folder structure in the remote server
            shell_script.append( ",".join(cac_mkdir).replace(',',' ') )
            
            
            #print args
            #retcode=subprocess.Popen(args)
            copy_run_files=["scp",options.config_file, analysis_file,
                            cac_server+':'+analysis.remote_root_dir]
            # Run the following command to start analysis.
            # Load modules
            run_cmd = ["ssh",cac_server,'\''+'source', cac_bash_profile+';',call_python,call_pipe,
                       '-r',os.path.join(analysis.remote_root_dir,config_file_name),
                       '-a',os.path.join(analysis.remote_root_dir,analysis_file_name),
                       irun_at,
                       '>',os.path.join(analysis.remote_root_dir,log_file_name)+'\'']
            
            shell_script.append( ",".join(copy_run_files).replace(',',' ') )
            # Uncomment
            #shell_script.append( ",".join(run_cmd).replace(',',' ') )
            
            # Write the shell script.
            shell_file.write(",".join(shell_script).replace(',','\n'))
            shell_file.close()
            
            # Run the shell script
            retcode=subprocess.Popen(["bash",shell_file_name])
                        
            if retcode == 0:
                subprocess.Popen(["mv",analysis_file,analysis_file+'.submitted'])
                
        else:
            logging.info("Analysis file %s was not submitted because not all sequences were found" % (analysis_file))
            pass
        

if __name__ == '__main__': main()